﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Brettle.Web.NeatUpload;
using System.IO;

public partial class Lib_ezModule_ezUpload : System.Web.UI.UserControl
{


    #region 属性
    /// <summary>
    /// 上传控件
    /// </summary>
    public InputFile inputFile
    {
        get { return InputFile1; }
    }

    /// <summary>
    /// 预览地址
    /// </summary>
    public string PreViewUrl
    {
        get { return "~"+ hyUrl.NavigateUrl; }
        set { hyUrl.NavigateUrl = value; }
    }

    /// <summary>
    /// 值
    /// </summary>
    public string Value
    {
        get
        {
            string _value="";
            if (DefaultMode == ModeState.Upload)
            {
                _value = GetRandFileName;
            }
            else
            {
                if (this.ViewState["Value"] != null)
                {
                    _value = this.ViewState["Value"].ToString();
                }                
            }
            return _value;
        }
        set {
            if (value != "")
            {
                DefaultMode = ModeState.View;
            }
            this.ViewState["Value"] = value; 
        }
    }

    /// <summary>
    /// 表单模式
    /// </summary>
    public enum ModeState
    {
        /// <summary>
        /// 上传
        /// </summary>
        Upload,
        /// <summary>
        /// 查看
        /// </summary>
        View,
        /// <summary>
        /// 默认
        /// </summary>
        Default
    }


    /// <summary>
    /// 现在模式
    /// </summary>
    public ModeState DefaultMode
    {
        get
        {
            ModeState mode = ModeState.Upload;
            if (this.ViewState["ModState"] != null)
            {
                mode = (ModeState)this.ViewState["ModState"];
            }
            return mode;
        }
        set { this.ViewState["ModState"] = value; }
    }

    /// <summary>
    /// 随机文件名
    /// </summary>
    public string GetRandFileName
    {
        get { return RandFileName(); }
    }

    #endregion



    /// <summary>
    /// 生成随机文件名
    /// </summary>
    /// <returns></returns>
    private string RandFileName()
    {
        string fp = "";
        if (InputFile1.HasFile)
        {
            //获取随机文件名
            fp = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
            string FileExt = System.IO.Path.GetExtension(InputFile1.FileName);
            fp += FileExt;
        }
        return fp;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (DefaultMode == ModeState.View)
            {
                LoadData();
            }
        }
    }    


    /// <summary>
    /// 设置可见控件
    /// </summary>
    private void SetVisible()
    {
        switch (DefaultMode)
        {
            case ModeState.Upload:
                Panel1.Visible = false;
                Panel2.Visible = true;
                break;
            case ModeState.View:
                Panel1.Visible = true;
                Panel2.Visible = false;
                break;
            case ModeState.Default:
                break;
        }
    }


    /// <summary>
    /// 显示查看模式
    /// </summary>
    public void LoadData()
    {
        SetVisible();

        lblFileName.Text = Value;
        hyUrl.NavigateUrl = PreViewUrl;

        lblMsg.Text = "";
    }
    protected void linkAction_Click(object sender, EventArgs e)
    {
        DefaultMode = ModeState.Upload;
        SetVisible();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //删除文件        
        if (DeleteFile())
        {
            DefaultMode = ModeState.Upload;
            Value = "";
            SetVisible();
            lblMsg.Visible = true;
            lblMsg.Text = "删除文件成功!";
        }
        else
        {
            lblMsg.Visible = true;
            lblMsg.Text = "删除文件失败!";
        }
    }

    /// <summary>
    /// 删除文件
    /// </summary>
    /// <returns></returns>
    private bool DeleteFile()
    {
        bool btn = false;
        if (PreViewUrl != "")
        {
            string path = Server.MapPath(PreViewUrl);
            if (File.Exists(path))
            {
                try
                {
                    File.Delete(path);
                    btn = true;
                }
                catch(System.Exception err) {
                    string msg = err.Message;
                }
            }
        }
        return btn;
    }
}
